package com.huayun.dcsp.service2.service;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline; 




import com.huayun.dcsp.common.database.DatabaseContextHolder;
import com.huayun.dcsp.common.jedis.JedisClient;
import com.huayun.dcsp.common.jedis.JedisSource;
import com.huayun.dcsp.service2.model.*;
import com.huayun.dcsp.service2.dao.StudentDao;

@Service("studentService")   
@Transactional 
@PropertySource(value = "classpath:config.properties")
public class StudentServiceImpl implements StudentService {
	
	
	//自动注入dao
	@Resource(name = "studentDao")
	private StudentDao studentDao;
	/*
	public void setStudentDao(StudentDao studentDao) {   
		 this.studentDao = studentDao;   
	}
	*/
	
	//自动注入jedisClientUtil
	@Resource(name = "jedisClient")
	private JedisClient  jedisClient;
	
	
	//自动注入RedisUrl
	//@Value("#{map['server.ip']}") 
	@Value("${server.ip:127.0.0.1}")
	private String redisUrl;
	
	@Override
	public  List<Student> getAllStudents() {		
		List<Student> list=studentDao.getAllStudent();
		return list;
	}
		
	@Override
	public Student getStudent(String id){
		return studentDao.getStudentById(id);
	}
	
	@Override
	public List<Map<String, Object>> queryStudents(String condition){
		
		System.out.println("redisUrl1 is "+redisUrl);
		List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
		ObjectMapper mapper = new ObjectMapper();
		
		try{
			System.out.println("conditon is "+jedisClient.get("condition"));
			if(condition.equals(jedisClient.get("condition"))){
				//调用JedisClientUtil，简化设置key、value
				list= mapper.readValue(jedisClient.get("students"), List.class);  
				System.out.println("student1 is "+jedisClient.get("condition")+" "+jedisClient.get("students"));  	
			
			}else
			{
				//多数据库情况下，指定数据源进行操作
				DatabaseContextHolder.setDBType("oracleDataSource");
				list=studentDao.queryStudents(condition);
				
				//将json写入redis中缓存,直接使用JedisDataSource，可以进行批量操作
				String jsonfromList = mapper.writeValueAsString(list);
				Jedis jedis = null;
		        try {
		            jedis = jedisClient.getJedisSource().getJedisInstance();
		             
		            Pipeline p = jedis.pipelined();
		            p.set("condition", condition);
		            p.set("students",jsonfromList);
		            p.sync();
		            System.out.println("student2 is "+jedisClient.get("condition")+" "+jsonfromList);
		        } finally {
		        	jedisClient.getJedisSource().returnResource(jedis);
		        }
				
			}
		}catch (Exception e) {
            e.printStackTrace();
		}
	
		
		return list;
	}

}
